package com.xlp.snake.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.text.StyledEditorKit.BoldAction;

import com.xlp.snake.listener.SnakeListener;
import com.xlp.snake.util.Global;

public class Snake {
	//表示方向的常量
	public static final int UP = -1;
	public static final int DOWN = 1;
	public static final int LEFT = 2;
	public static final int RIGHT = -2;
	
	private int oldDirection,newDirection;
	
	
	
	private Point oldTail;
	public static boolean a=true;
    public static int cc =1; 
    public static int sudu = 320;
	//表示蛇坐标
	private LinkedList<Point> body = new LinkedList<Point>();
	
	// 监听器组,可以注册多个监听器
	private Set<SnakeListener> listeners = new HashSet<SnakeListener>(); 

    public static boolean life;
    
    //表示暂停
    public static boolean pause =  true;
		
	public Snake() {
		//初始化的方法
		init();
	}
	
	public void init() {
		//显示区中间
		int x = Global.WIDTH / 2;
		int y = Global.HEIGHT / 2;
		//初始化蛇的长度
		for(int i = 0; i< 3;i++) {
		    body.addLast(new Point(x--,y));;
		}
		//方向默认向右
		oldDirection = newDirection = RIGHT;
		life = true;		  
	}
	
	public void die() {
		life = false;
	}
	
	//蛇移动的方法
	public void move() {
		System.out.println("Snake's move");
		//忽略相反方向
		if(!(oldDirection + newDirection == 0)) {
			oldDirection = newDirection;
		}
		
		//去尾
		oldTail = body.removeLast();
		
		//得到原来蛇头的坐标
		int x = body.getFirst().x;
		int y = body.getFirst().y;
		
		//根据方向计算出新坐标
		switch(oldDirection) {
		case UP:
			y--; // 向上y坐标减一
			if(y < 0) {
				y = Global.HEIGHT - 1;  //从一边出去，另一边出来
			}
			break;
		case DOWN:
			y++;
			if(y >= Global.HEIGHT ) {
				y = 0;
			}
			break;
		case LEFT:
			x--;
			if(x < 0) {
				x = Global.WIDTH - 1;
			}
			break;
		case RIGHT:
			x++;
			if(x >= Global.WIDTH) {
				x = 0;
			}
			break;		
		}
		//新蛇头坐标	
		Point newHead = new Point(x,y); 
		
		//加头
		body.addFirst(newHead);
	}
    
	//蛇改变方向的方法
	public void changeDirection(int direction) {
		System.out.println("Snake's changeDirection");

		newDirection = direction;  //去除无效方向
	}

	//蛇吃食物的方法
	public void eatFood() {
		System.out.println("Snake's eatFood");
		
		//吃到食物加上原来的蛇尾
		body.addLast(oldTail); 
	}
    
	//蛇是否吃到身体的方法
	public boolean isEatBody() {
		System.out.println("Snake's isEatBody");
		
		for(int i = 1;i < body.size();i++) {
			if(body.get(i).equals(this.getHead())) {
				return true;
			}
		}
		return false;
	}
  
	//显示的方法
	public void drawMe(Graphics g) {
		System.out.println("Snake's drawMe");
		
		g.setColor(Color.white);
		//遍历集合
		for(Point p : body) {
			//填充3D的矩形
			g.fill3DRect(p.x * Global.CELL_SIZE, p.y * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
		}
	}
	
	//得到蛇头结点的方法
	public Point getHead() {
		return body.getFirst();
	}
	public void zanting() {
		cc++;
	}

	//蛇不停移动的方法
	private class SnakeDriver implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (life) {
				if(pause) {  //为true则移动，为假则暂停
					move();
				}
				// 蛇移动后触发事件，遍历listeners中的元素
				for (SnakeListener e : listeners) {
					e.snakeMoved(Snake.this);
				}
				try {
					Thread.sleep(sudu);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(cc==1) {
				if(a==true) {
					life = false;  
				}
			  }
			}

		}

	}
	//加速
	public static void jiasu() {
		if(sudu>=100) {
			sudu-=25;
		}
	}
	
	//减速
	public static void jiansu() {
		if(sudu<=300) {
			sudu+=25;
		}
	}

	// 启动线程的方法
	public void start() {
		new Thread(new SnakeDriver()).start();
	}

	// 添加监听器的方法
	public void addSnakeListener(SnakeListener e) {
		if (e != null) {
			this.listeners.add(e);
		}
	}
    
}
